package com.qidian.hw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.qidian.hw.dao.ProductDAO;
import com.qidian.hw.pojo.Product;
import com.qidian.hw.pojo.ServiceInfo;
import com.qidian.hw.tools.FormatUtil;

/**
* @author 郭浩
*
*@company 起点编程
*
* 2022年8月19日 下午6:05:32
*/
public class ProductDAOImpl extends BaseDAO implements ProductDAO{
	private Connection con;
	private PreparedStatement pstmt;
	private ResultSet rs;
	public ArrayList<Product> productList() {
		ArrayList<Product> al = new ArrayList<Product>();
		String sql = "select * from product where status = 1";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				Product product = new Product();
				product.setProductId(rs.getLong("product_id"));
				product.setProductTitle(rs.getString("product_title"));
				product.setProductDesc(rs.getString("product_desc"));
				product.setSolutionImg(rs.getString("solution_img"));
				product.setProductHref(rs.getString("product_href"));
				product.setTypeDesc(rs.getString("type_desc"));
				product.setCreateTime(rs.getString("create_time"));
				product.setModifyTime(rs.getString("modify_time"));
				al.add(product);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs,pstmt,con);
		}
		return al;
	}

	public Product queryProductById(long id) {
		Product product = null;
		String sql = "select * from product where product_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, id);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				product = new Product();
				product.setProductTitle(rs.getString("product_title"));
				product.setProductDesc(rs.getString("product_desc"));
				product.setSolutionImg(rs.getString("solution_img"));
				product.setProductHref(rs.getString("product_href"));
				product.setTypeDesc(rs.getString("type_desc"));
				product.setCreateTime(rs.getString("create_time"));
				product.setModifyTime(rs.getString("modify_time"));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs,pstmt,con);
		}
		return product;
	}

	public int productUpdate(Product product) {
		int result = 0;
		String sql = "update product set product_title = ?,product_desc = ?,solution_img = ?,product_href = ?,type_desc = ?,modify_time = ? where product_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, product.getProductTitle());
			pstmt.setString(2, product.getProductDesc());
			pstmt.setString(3, product.getSolutionImg());
			pstmt.setString(4, product.getProductHref());
			pstmt.setString(5, product.getTypeDesc());
			String date = FormatUtil.getNow();
			pstmt.setString(6, date);
			pstmt.setLong(7, product.getProductId());
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs,pstmt,con);
		}
		return result;
	}

	public int productDelete(long id) {
		int result = 0;
		String sql = "update product set modify_time = ?,status = 0 where product_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			String date = FormatUtil.getNow();
			pstmt.setString(1, date);
			pstmt.setLong(2, id);
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs,pstmt,con);
		}
		return result;
	}

	public int productSave(Product product) {
		int result = 0;
		String sql = "insert into product (product_title,product_desc,solution_img,product_href,type_desc,create_time,modify_time) values(?,?,?,?,?,?,?)";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, product.getProductTitle());
			pstmt.setString(2, product.getProductDesc());
			pstmt.setString(3, product.getSolutionImg());
			pstmt.setString(4, product.getProductHref());
			pstmt.setString(5, product.getTypeDesc());
			String date = FormatUtil.getNow();
			pstmt.setString(6, date);
			pstmt.setString(7, date);
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs,pstmt,con);
		}
		return result;
	}

	public int count(String title) {
		int result = 0;
		String sql = "select count(*) from product where status = 1  ";
		// 拼接条件
		if (FormatUtil.isNull(title)) {
			sql += " and product_title like '%" + title + "%'";
		}
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

	public ArrayList<Product> queryByPage(int page, int size, String title) {
		ArrayList<Product> products = new ArrayList<Product>();
		String sql = "select * from product where status = 1 ";
		// 拼接条件
		if (FormatUtil.isNull(title)) {
			sql += " and product_title like '%" + title + "%'";
		}
		sql += " limit "+((page-1)*size)+","+size;
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				Product p = new Product();
				p.setProductId(rs.getLong("product_id"));
				p.setProductTitle(rs.getString("product_title"));
				p.setProductDesc(rs.getString("product_desc"));
				p.setSolutionImg(rs.getString("solution_img"));
				p.setProductHref(rs.getString("product_href"));
				p.setTypeDesc(rs.getString("type_desc"));
				p.setCreateTime(rs.getString("create_time"));
				p.setModifyTime(rs.getString("modify_time"));
				p.setStatus(rs.getInt("status"));
				products.add(p);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return products;
	}
	
	public int deletes(String[] ids) {
		int result = 0;
		String sql = "update product set status = 0 where product_id in(";
		for(int i = 0;i < ids.length;i ++) {
			sql += ids[i] + ",";
		}
		sql += "0)";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			result = pstmt.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}
}
